package ltd.flyweight.liquibasehelper.module.zngk.service;

import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ltd.flyweight.liquibasehelper.module.jzgk.dao.dbservice.JzgkSysRegionDbService;
import ltd.flyweight.liquibasehelper.module.jzgk.dao.entity.JzgkSysRegionEntity;
import ltd.flyweight.liquibasehelper.module.zngk.dao.dbservice.ZngkSysDeptDbService;
import ltd.flyweight.liquibasehelper.module.zngk.dao.dbservice.ZngkSysRegionDbService;
import ltd.flyweight.liquibasehelper.module.zngk.dao.entity.ZngkSysDeptEntity;
import ltd.flyweight.liquibasehelper.module.zngk.dao.entity.ZngkSysRegionEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class ZngkSysRegionService {

    private final ZngkSysRegionDbService zngkSysRegionDbService;
    private final ZngkSysDeptDbService zngkSysDeptDbService;

    private final JzgkSysRegionDbService jzgkSysRegionDbService;

    public void compare() {

        List<ZngkSysRegionEntity> zngkSysRegionList = zngkSysRegionDbService.list();

        Map<Long, ZngkSysRegionEntity> zngkSysRegionIdMap = CollectionUtil.toMap(zngkSysRegionList, new LinkedHashMap<>(), ZngkSysRegionEntity::getId);
        Map<String, ZngkSysRegionEntity> zngkSysRegionCodeMap = CollectionUtil.toMap(zngkSysRegionList, new LinkedHashMap<>(), ZngkSysRegionEntity::getCode);

        if (zngkSysRegionCodeMap.keySet().size() != zngkSysRegionList.size()) {
            throw new RuntimeException("唯一Id重复");
        }
        List<JzgkSysRegionEntity> jzgkSysRegionList = jzgkSysRegionDbService.list();

        Map<String, JzgkSysRegionEntity> jzgkSysRegionCodeMap = CollectionUtil.toMap(jzgkSysRegionList, new LinkedHashMap<>(), JzgkSysRegionEntity::getId);

        Collection<String> inZngkNotInJzgkCodes = CollectionUtil.subtract(zngkSysRegionCodeMap.keySet(), jzgkSysRegionCodeMap.keySet());
        Collection<String> inJzgkNotInZngkCodes = CollectionUtil.subtract(jzgkSysRegionCodeMap.keySet(), zngkSysRegionCodeMap.keySet());

        log.info(
                "zngkSysRegionList.size()={},jzgkSysRegionList.size()={},inZngkNotInJzgkIds.size()={},inJzgkNotInZngkIds.size()={}",
                zngkSysRegionList.size(),
                jzgkSysRegionList.size(),
                inZngkNotInJzgkCodes.size(),
                inJzgkNotInZngkCodes.size()
        );

        Map<String, JzgkSysRegionEntity> jzgkSysRegionExtNameMap = CollectionUtil.toMap(jzgkSysRegionList, new LinkedHashMap<>(), JzgkSysRegionEntity::getExtName);

        List<String> ids = new ArrayList<>();

        for (String code : inZngkNotInJzgkCodes) {
            ZngkSysRegionEntity zngkSysRegionEntity = zngkSysRegionCodeMap.get(code);

            JzgkSysRegionEntity jzgkSysRegionEntity = jzgkSysRegionExtNameMap.get(zngkSysRegionEntity.getName());
            if (jzgkSysRegionEntity != null) {
                if (code.startsWith(jzgkSysRegionEntity.getId())) {
                    ids.add(code);
                    continue;
                }
            }
            // log.info("{}", zngkSysRegionEntity);
        }

        List<ZngkSysDeptEntity> zngkSysDeptEntityList = zngkSysDeptDbService.list();
        for (ZngkSysDeptEntity zngkSysDeptEntity : zngkSysDeptEntityList) {
            if (StringUtils.isNotBlank(zngkSysDeptEntity.getDistrictCode())) {
                ZngkSysRegionEntity zngkSysRegionEntity = zngkSysRegionIdMap.get(Long.parseLong(zngkSysDeptEntity.getDistrictCode()));
                if (zngkSysRegionEntity == null) {
                    log.info("***找不到智能管控系统行政区划***：{}", zngkSysDeptEntity);
                    continue;
                }
                JzgkSysRegionEntity jzgkSysRegionEntity = jzgkSysRegionCodeMap.get(zngkSysRegionEntity.getCode());
                if (jzgkSysRegionEntity == null) {
                    log.info("===找不到集中管控系统行政区划===：{}", zngkSysDeptEntity);
                    continue;
                }

            }
        }
    }

}
